import  invariant from "invariant";
import {observablePromise} from 'bggood-store-utils';
/**
 * action操作方法
 * 
 * @export
 * @class PromiseAction
 */
export class PromiseAction{
    constructor(options){
      options=options||{};
      return this;
    }
    init(dispatch,type){
      invariant(typeof dispatch==='object', 'dispatch: dispatch should be a object');
      invariant(typeof dispatch.commit==='function', 'dispatch.commit: dispatch.commit should be a function');
      invariant(typeof type==='string', 'type: dispatch.commit should be a string');
      dispatch.commit(type);
      return this;
    }
    set(dispatch,type,promise){
      invariant(typeof dispatch==='object', 'dispatch: dispatch should be a object');
      invariant(typeof dispatch.commit==='function', 'dispatch.commit: dispatch.commit should be a function');
      invariant(typeof type==='string', 'type: dispatch.commit should be a string');
      observablePromise(promise,(item)=>{
        dispatch.commit(type,item);
      });
      return this;
    }
    commit(promise,callBackFunc){
      observablePromise(promise,(item)=>{
        callBackFunc&&callBackFunc(item);
      });
      return this;
    }
  }